<template>
  <div>
    <el-descriptions title="个人信息" direction="vertical" :column="4" border style="margin-top: 30px">
      <el-descriptions-item label="用户ID">
        {{ userOne.id }}
      </el-descriptions-item>
      <el-descriptions-item label="登录名">{{ userOne.username }}</el-descriptions-item>
      <el-descriptions-item label="密码 ">{{ userOne.password }}</el-descriptions-item>
      <el-descriptions-item label="用户名" :span="2">{{ userOne.name }}</el-descriptions-item>
      <el-descriptions-item label="用户类型">
        {{ userOne.type == 0 ? '管理员' : userOne.type == 1 ? '普通用户' : userOne.type == 2 ? '车库管理员' : '未知' }}
      </el-descriptions-item>
      <el-descriptions-item label="操作">
        <el-link type="primary" @click="toEdit()">修改个人信息</el-link>
      </el-descriptions-item>
    </el-descriptions>

    <el-dialog
      title="提示"
      :visible.sync="dialog"
      width="40%"
    >
      <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户ID" prop="id">
          <el-input v-model="userForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="登录名" prop="username">
          <el-input v-model="userForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userForm.password"></el-input>
        </el-form-item>
        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="userForm.name"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" prop="type" disabled="disabled">
          <el-select v-model="userForm.type" disabled>
            <el-option label="管理员" :value="0"></el-option>
            <el-option label="普通用户" :value="1"></el-option>
            <el-option label="车库管理员" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('userForm')">提交</el-button>
          <el-button @click="resetForm('userForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {saveOrUpdate, showOne, validate} from "@/api/user";

export default {
  name: "index",
  data() {
    var validateUserName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入登录名'));
      } else {
        //发送请求
        validate({username: value, id: this.userForm.id}).then(res => {
          callback();
        })

      }
    };
    return {
      uname: '',
      userOne: {},
      dialog: false,
      userForm: {
        id: '',
        username: '',
        password: '',
        name: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入登录名', trigger: 'blur'},
          {validator: validateUserName, trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入用户密码', trigger: 'blur'},
        ],
        name: [
          {required: true, message: '请输入用户姓名', trigger: 'blur'},
        ],
      }
    }
  },
  created() {
    this.uname = sessionStorage.getItem("username")
    this.show();
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveOrUpdate(this.userForm).then(res => {
            if (res.code == 20000) {
              this.$message.success("修改成功")
              this.dialog = false;
              this.show()
            }
          })
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toEdit() {
      this.$set(this.userForm, 'id', this.userOne.id)
      this.$set(this.userForm, 'username', this.userOne.username)
      this.$set(this.userForm, 'password', this.userOne.password)
      this.$set(this.userForm, 'name', this.userOne.name)
      this.$set(this.userForm, 'type', this.userOne.type)
      this.dialog = true;
    },
    show() {
      showOne(this.uname).then(res => {
        if (res.code === 20000) {
          this.userOne = res.data
        }
      })
    }
  }
}
</script>
<style scoped>

</style>
